﻿
Namespace DBase

    Public Class Conv

        Public Shared Function StrToDB(ByVal value As String) As String
            value = value.Replace("’", "'")
            Return "'" & value.Replace("'", "''") & "'"

        End Function
        Public Shared Function IntToDB(ByVal value As Integer) As String
            Return value.ToString()
        End Function
        Public Shared Function LongToDB(ByVal value As Long) As String
            Return value.ToString()
        End Function
        Public Shared Function DoubleToDB(ByVal value As Double) As String
            'Return value.ToString(New Globalization.CultureInfo("en-US"))
            Return value.ToString.Replace(",", ".")

        End Function
        Public Shared Function BoolToDB(ByVal value As Boolean) As Integer
            Return IIf(value, 1, 0)
        End Function
        Public Shared Function DateToDB(ByVal o As Date) As Object
            Return IIf(o = Nothing, DBNull.Value, o)

        End Function
        Public Shared Function DateFromDB(d As Global.MySql.Data.Types.MySqlDateTime) As Date
            If d.Year = 0 Then
                Return Date.MinValue
            Else
                Return d.Value
            End If
        End Function
        Public Shared Function TimeToDB(ByVal o As Object) As String
            Dim d = Utils.Conv.ToDate(o, Nothing)

            Return Format(d, "HH:mm")

        End Function

        Public Shared Function ListToDB(l As IList) As String
            Dim s As New Text.StringBuilder


            If l.Count > 0 Then
                For Each o In l
                    If o.GetType Is GetType(String) Then
                        s.Append(DBase.Conv.StrToDB(o))
                    Else
                        s.Append(DBase.Conv.DoubleToDB(o))
                    End If
                    s.Append(",")
                Next

                s.Remove(s.Length - 1, 1)

                s.Insert(0, "(")
                s.Append(")")
            End If

            Return s.ToString

        End Function
        Public Shared Function FieldToAlias(f As String) As SqlField
            Dim sf As New SqlField

            Dim s = Split(f, "as", , CompareMethod.Text)

            sf.Name = s(0).Trim
            If s.Length > 1 Then
                sf.Alias = s(1).Trim
            End If

            Return sf

        End Function

        ' ''' <summary>
        ' ''' in funzione del tipo dato -> ritorna la stringa sqll
        ' ''' </summary>
        ' ''' <param name="value"></param>
        ' ''' <returns></returns>
        ' ''' <remarks></remarks>
        'Public Shared Function ValToSql(ByVal value As Object) As String
        '    '******************
        '    If IsDBNull(value) Then Return "Null"
        '    Dim dbtype As System.Data.DbType
        '    dbtype = VarTypeToType(Type.GetTypeCode (value.GetType))
        '    Select Case dbtype
        '        Case System.Data.DbType.Date, System.Data.DbType.DateTime
        '            System.Diagnostics.Debug.Assert(False) 'dobbiamo implementare datatosql/DB
        '            Return CType(value, DateTime).ToShortDateString
        '        Case System.Data.DbType.Boolean
        '            Return BoolToDB(CType(value, Boolean))
        '        Case System.Data.DbType.Decimal, System.Data.DbType.Double
        '            Return DoubleToDB(CType(value, Double))
        '        Case System.Data.DbType.AnsiString, System.Data.DbType.AnsiStringFixedLength, System.Data.DbType.String, System.Data.DbType.StringFixedLength
        '            Return StrToDB(value.ToString)
        '        Case Else
        '            Return value.ToString
        '    End Select

        'End Function
        'Public Shared Function VarTypeToType(ByVal VarType As TypeCode) As System.Data.DbType

        '    Select Case VarType
        '        Case TypeCode.Boolean
        '            Return DbType.Boolean
        '        Case TypeCode.Byte
        '            Return DbType.Byte
        '        Case TypeCode.SByte
        '            Return DbType.SByte
        '        Case TypeCode.Char
        '            Return DbType.String
        '        Case TypeCode.DateTime
        '            Return DbType.DateTime
        '        Case TypeCode.Decimal
        '            Return DbType.Decimal
        '        Case TypeCode.Double
        '            Return DbType.Double
        '        Case TypeCode.Single
        '            Return DbType.Single
        '        Case TypeCode.Int16
        '            Return DbType.Int16
        '        Case TypeCode.UInt16
        '            Return DbType.UInt16
        '        Case TypeCode.Int32
        '            Return DbType.Int32
        '        Case TypeCode.UInt32
        '            Return DbType.UInt32
        '        Case TypeCode.Int64
        '            Return DbType.Int64
        '        Case TypeCode.UInt64
        '            Return DbType.UInt64
        '        Case TypeCode.String
        '            Return DbType.String
        '        Case TypeCode.DBNull
        '            'Throw New Exception("DBChe?")
        '            ' da verificare
        '            Return DbType.String
        '        Case Else
        '            Throw New Exception("UnManaged TypeCode")
        '    End Select

        'End Function
    End Class
    Public Class SqlField
        Public Name As String
        Public [Alias] As String
    End Class
    Public Class Data



        Public Shared Function GetListPropObjectDataTable(ByRef dt As DataTable, ByVal t As System.Type) As List(Of Reflection.PropertyInfo)

            Dim PropList As New List(Of Reflection.PropertyInfo)
            Dim PropTemp As Reflection.PropertyInfo
            For Each c As DataColumn In dt.Columns
                PropTemp = t.GetProperty(c.ColumnName, Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase)
                If PropTemp IsNot Nothing Then
                    PropList.Add(PropTemp)
                End If
            Next

            Return PropList

        End Function
        Public Shared Sub DataRowToObject(ByRef dr As DataRow, ByRef obj As Object, ByRef PropList As List(Of Reflection.PropertyInfo))

            For ct As Integer = 0 To PropList.Count - 1
                If dr(PropList(ct).Name) IsNot DBNull.Value Then
                    If PropList(ct).PropertyType = dr(PropList(ct).Name).GetType Then
                        PropList(ct).SetValue(obj, dr(PropList(ct).Name), Nothing)
                    Else
                        'DirectCast(dr(PropList(ct).Name),MySql.Data.Types.MySqlDateTime)
                        If PropList(ct).PropertyType Is GetType(DateTime) Then
                            If dr(PropList(ct).Name).year = 0 Then
                                PropList(ct).SetValue(obj, DateTime.MinValue, Nothing)
                            Else
                                PropList(ct).SetValue(obj, Convert.ChangeType(dr(PropList(ct).Name), PropList(ct).PropertyType), Nothing)
                            End If
                        Else
                            PropList(ct).SetValue(obj, Convert.ChangeType(dr(PropList(ct).Name), PropList(ct).PropertyType), Nothing)
                        End If
                    End If
                End If
            Next

        End Sub

        'Public Shared Function GetDBCacheContext(ByVal DBName As String) As DbLinq.Data.Linq.DataContext
        '    Dim t = Type.GetType("ww")

        '    '            Dim o As New t.gettype


        '    Dim db_cont = Web.HttpContext.Current.Items(DBName)
        '    If db_cont Is Nothing Then
        '        Dim cnn As New Global.MySql.Data.MySqlClient.MySqlConnection( _
        '            System.Web.Configuration.WebConfigurationManager.ConnectionStrings(DBName).ToString)


        '        'db_cont = New MyDBase.UrbiStat(cnn)
        '        db_cont = New DbLinq.Data.Linq.DataContext(cnn, New DbLinq.MySql.MySqlVendor)

        '        'db_cont.Log = IO.File.AppendText("e:\aa.txt") '  New IO.FileStream("e:\aa.txt", IO.FileMode.Open, IO.FileAccess.Write, IO.FileShare.Read).
        '        'db_cont.Log = New IO.StreamWriter("e:\aa.txt") With {.AutoFlush = True}
        '        db_cont.Log = New IO.StringWriter

        '        db_cont.ObjectTrackingEnabled = True

        '        Web.HttpContext.Current.Items(DBName) = db_cont
        '    End If

        '    Return DirectCast(db_cont, DbLinq.Data.Linq.DataContext)

        'End Function
    End Class

    'Namespace Linq

    '    Public Class DataRef(Of TResult As {New, Class})
    '        Inherits List(Of TResult)
    '        Public t As Table(Of TResult)


    '        Public Overloads Sub add(e As TResult)
    '            MyBase.Add(e)

    '            t.InsertOnSubmit(e)

    '        End Sub

    '    End Class


    '    Public Class DataRef_old(Of TResult As {New, Class})
    '        Implements IEnumerable(Of TResult)

    '        Private l As List(Of TResult)
    '        Private t As Table(Of TResult)

    '        'Default Public ReadOnly Property item(n As Integer) As TResult
    '        '    Get
    '        '        Return Me.l(n)
    '        '    End Get
    '        'End Property

    '        Public Sub New(l As List(Of TResult), t As Table(Of TResult))

    '            Me.l = l
    '            Me.t = t

    '        End Sub

    '        Public Sub Add(e As TResult)

    '            l.Add(e)
    '            t.InsertOnSubmit(e)


    '        End Sub
    '        Public Sub Remove(e As TResult)

    '            l.Remove(e)
    '            t.DeleteOnSubmit(e)

    '        End Sub
    '        Public Sub Clear()

    '            t.DeleteAllOnSubmit(l)
    '            l.Clear()

    '        End Sub
    '        Public Function ToList() As List(Of TResult)

    '            Return l

    '        End Function


    '        Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of TResult) Implements System.Collections.Generic.IEnumerable(Of TResult).GetEnumerator
    '            Return Me.l.GetEnumerator
    '        End Function

    '        Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
    '            Return Me.l.GetEnumerator()

    '        End Function
    '    End Class
    'End Namespace
End Namespace
